// pages/index/detail/detail.js
const { indexDetail,indexCollect,indexLike,indexFollow,indexTeamDetail,indexTeamJoin } = require('../../../utils/api.js')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    item:[],
    projectId:null,
    ownerId:null,
    collectionStatus:NaN,
    likeStatus:NaN,
    followStatus:NaN,
    flag:false,
    team: {},
    members:[]
  },
  onChange(event) {
    let that = this
    let show = that.data.show
    that.setData({
      show: !show
    })
  },
  // 加入团队
  join(){
    let that = this
    let show = that.data.show
    let userId = app.globalData.userId
    console.log('userID',userId);
    let teamId = that.data.team.id
    indexTeamJoin(teamId,userId).then(res=>{
      console.log('join',res);
      if(res.code === 200){
        wx.showToast({
          title: '加入成功',
          icon: 'success',
          duration:800,
        })
      }else{
        wx.showToast({
          title: '加入失败',
          icon: 'error',
          duration: 800
        })
      }
    }).then(()=>{
      setTimeout(()=>{
        that.setData({
          show: !show
        })
      },900)
    })
  },
  collect(){
    let userId = app.globalData.userId;
    let projectId = this.data.projectId;
    indexCollect(userId,projectId).then((res)=>{
      this.setData({
        collectionStatus:Number(res.data.status)
      })
    })
    console.log('collectionStatus',this.data.collectionStatus);
  },
  like(){
    let userId = app.globalData.userId;
    let projectId = this.data.projectId;
    indexLike(userId,projectId).then((res)=>{
      this.setData({
        likeStatus:Number(res.data.status)
      })
    })
    console.log('likeStatus',this.data.likeStatus);
  },
  follow(){
    let userId = app.globalData.userId;
    let projectId = this.data.projectId;
    let ownerId = this.data.ownerId
    indexFollow(userId,projectId,ownerId).then((res)=>{
      this.setData({
        followStatus:Number(res.data.status)
      })
      console.log('follow',res);
    })
    console.log('followStatus',this.data.followStatus);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    indexDetail(options.projectId).then((res)=>{
      let {projectId,ownerId,collectionStatus,likeStatus,followStatus} = res.data
      console.log('详情',res);
      this.setData({
        item: res.data,
        projectId,
        ownerId,
        followStatus:Number(followStatus),
        collectionStatus:Number(collectionStatus),
        likeStatus:Number(likeStatus)
      })
    })
    //团队
    indexTeamDetail(options.projectId).then(res=>{
      console.log('团队',res);
      try{
        let team =  res.data.team
        let members = res.data.members
        this.setData({
          team,
          members
        })
      }catch(err){
        console.log(err);
      }
      
     
      
    })
  },
})